package com.bthdung.studios;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.bthdung.studios.lesson1.LessonOneActivity;
import com.bthdung.studios.lesson10.HeightMapTest;
import com.bthdung.studios.lesson11.SpriteMethodTest;
import com.bthdung.studios.lesson12.SplashActivity;
import com.bthdung.studios.lesson2.LessonTwoActivity;
import com.bthdung.studios.lesson3.LessonThreeActivity;
import com.bthdung.studios.lesson4.LessonFourActivity;
import com.bthdung.studios.lesson5.LessonFiveActivity;
import com.bthdung.studios.lesson6.LessonSixActivity;
import com.bthdung.studios.lesson7.LessonSevenActivity;
import com.bthdung.studios.lesson8.LessonEightActivity;
import com.bthdung.studios.lesson9.ShaderNiceActivity;
import com.bthdung.studios.R;

public class MainListOfContents extends ListActivity 
{
	private long back_pressed = 0;

	private static final String ITEM_IMAGE = "item_image";
	private static final String ITEM_TITLE = "item_title";
	private static final String ITEM_SUBTITLE = "item_subtitle";	
	private DisplayMetrics mMetrics = new DisplayMetrics();
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindowManager().getDefaultDisplay().getMetrics(mMetrics);

		// portrait landscape
		//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		//setTitle(R.string.toc);
		setContentView(R.layout.table_of_contents);
		
		// Initialize data
		final List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		final SparseArray<Class<? extends Activity>> activityMapping = new SparseArray<Class<? extends Activity>>();
		
		int i = 0;
		
		{//1
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_one);
			item.put(ITEM_TITLE, getText(R.string.lesson_one));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_one_subtitle));
			data.add(item);
			activityMapping.put(i++, LessonOneActivity.class);			
		}
		
		{//2
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_two);
			item.put(ITEM_TITLE, getText(R.string.lesson_two));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_two_subtitle));
			data.add(item);
			activityMapping.put(i++, LessonTwoActivity.class);
		}
		
		{//3
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_three);
			item.put(ITEM_TITLE, getText(R.string.lesson_three));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_three_subtitle));
			data.add(item);
			activityMapping.put(i++, LessonThreeActivity.class);
		}
		
		{//4
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_four);
			item.put(ITEM_TITLE, getText(R.string.lesson_four));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_four_subtitle));
			data.add(item);
			activityMapping.put(i++, LessonFourActivity.class);
		}
		
		{//5
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_five);
			item.put(ITEM_TITLE, getText(R.string.lesson_five));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_five_subtitle));
			data.add(item);
			activityMapping.put(i++, LessonFiveActivity.class);
		}
		
		{//6
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_six);
			item.put(ITEM_TITLE, getText(R.string.lesson_six));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_six_subtitle));
			data.add(item);
			activityMapping.put(i++, LessonSixActivity.class);
		}
		
		{//7
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_seven);
			item.put(ITEM_TITLE, getText(R.string.lesson_seven));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_seven_subtitle));
			data.add(item);
			activityMapping.put(i++, LessonSevenActivity.class);
		}
		
		{//8
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_eight);
			item.put(ITEM_TITLE, getText(R.string.lesson_eight));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_eight_subtitle));
			data.add(item);
			activityMapping.put(i++, LessonEightActivity.class);
		}
		
		{//9
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_nice);
			item.put(ITEM_TITLE, "9: " + getText(R.string.lesson_nice));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_nice_subtitle));
			data.add(item);
			activityMapping.put(i++, ShaderNiceActivity.class);
		}

		{//10
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_nice);
			item.put(ITEM_TITLE, "10: " + getText(R.string.lesson_nice));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_nice_subtitle));
			data.add(item);
			activityMapping.put(i++, HeightMapTest.class);
		}

		{//11
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_nice);
			item.put(ITEM_TITLE, "11: " + getText(R.string.lesson_nice));
			item.put(ITEM_SUBTITLE, getText(R.string.lesson_nice_subtitle));
			data.add(item);
			activityMapping.put(i++, SpriteMethodTest.class);
		}

		{//12
			final Map<String, Object> item = new HashMap<String, Object>();
			item.put(ITEM_IMAGE, R.drawable.ic_lesson_nice);
			item.put(ITEM_TITLE, "12: " + getText(R.string.lesson_nice));
			item.put(ITEM_SUBTITLE, "OpenGL 2.0 Terrain in Landscape mode tuttorials");
			data.add(item);
			activityMapping.put(i++, SplashActivity.class);
		}

		final SimpleAdapter dataAdapter = new SimpleAdapter(this, data, R.layout.toc_item, new String[] {ITEM_IMAGE, ITEM_TITLE, ITEM_SUBTITLE}, new int[] {R.id.Image, R.id.Title, R.id.SubTitle});
		setListAdapter(dataAdapter);	
		
		getListView().setOnItemClickListener(new OnItemClickListener() 
		{
			@Override
			 public void onItemClick(AdapterView<?> parent, View view,
				        int position, long id) 
			{
				final Class<? extends Activity> activityToLaunch = activityMapping.get(position);
				
				if (activityToLaunch != null)
				{
					final Intent launchIntent = new Intent(MainListOfContents.this, activityToLaunch);
					startActivity(launchIntent);
				}				
			}
		});
	}	

	@Override
	public void onBackPressed() {
		if (back_pressed + 1000 > System.currentTimeMillis()) {
			super.onBackPressed();
		} else {
			Toast.makeText(getBaseContext(), "Press again to exit!",
					Toast.LENGTH_SHORT).show();
		}
		back_pressed = System.currentTimeMillis();
	}
}
